<template>
  <el-dialog :close-on-click-modal="false" 
    :title="title"
    :visible="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="用户名" prop="UserName">
        <el-input v-model.trim="form.UserName" />
      </el-form-item>
      <el-form-item label="密码" prop="PassWord">
        <el-input v-model.trim="form.PassWord" type="password" />
      </el-form-item>
      <el-form-item label="电话" prop="PhoneNo">
        <el-input v-model.trim="form.PhoneNo" />
      </el-form-item>
      <el-form-item label="邮箱" prop="Email">
        <el-input v-model.trim="form.Email" />
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.Authority" placeholder="请选择">
          <el-option
            v-for="item in RoleInfo"
            :key="item.RoleName"
            :label="item.RoleName"
            :value="item.RoleName"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="角色" prop="roles">
        <el-checkbox-group v-model="form.roles">
          <el-checkbox label="admin" />
          <el-checkbox label="editor" />
        </el-checkbox-group>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { RoleManagerInfo, Newly, Renew } from '@/api/userManagement'
  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        juese: '',
        form: {
          UserId: '',
          Password: '',
          UserName: '',
          Authority: '',
          Sex: '',
          PhoneNo: '',
          Email: '',
          Creator: '',
          CreateTime: new Date(),
        },
        rules: {
          UserId: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          Password: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          Authority: [
            { required: true, trigger: 'blur', message: '请选择角色' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        RoleInfo: [],
      }
    },
    mounted() {
      this.getRoleInfo()
    },
    methods: {
      getRoleInfo() {
        RoleManagerInfo({ Index: 0, Count: 100 }).then((res) => {
          this.RoleInfo = res.RoleInfo
        })
      },
      showEdit(row) {
        console.log('rowrow', row)
        this.getRoleInfo()
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title == '编辑') {
              await Renew(this.form)
            } else {
              this.form.UserId = 'UID' + new Date().getTime()
              this.form.Sex = '男'
              await Newly(this.form)
            }
            this.$baseMessage('操作成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
